
import engine from "../../../Engine";
import { AdsBase } from "../../AdsBase";
import { RecorderBase } from "../../RecorderBase";
import { CCComFun } from "../../util/CCComFun";
import { CCGlobal, VideoCall } from "../../util/CCGlobal";


export class kuaishouAdsFunc extends AdsBase {
    private videoAd: video = null;
    private interstitialAd: interstitial = null;


    public init(): void {
        let interstitialId = CCGlobal.Config.interstitialId; // 插屏广告ID

        let videoId = CCGlobal.Config.videoId;
        if (videoId) {
            this.videoAd = new video(videoId);
            this.videoAd.create();
        }


        if (interstitialId) {
            this.interstitialAd = new interstitial(interstitialId);
            this.interstitialAd.create();
        }

    }


    public loadVideoAd(opt: VideoCall): void {
        if (this.videoAd.videoShowing) {
            return CCComFun.CCLog("视频展示中,本次视频按钮点击无效");
        }
        if (!this.videoAd) {
            this.showToast();
            return opt.errCallback && opt.errCallback();
        }
        this.videoAd.adCall = opt;
        this.videoAd.show();
    }

    public shareAppMessage(opt: VideoCall) {
        CCGlobal.API.shareAppMessage({
            success: () => {
                opt && opt.suCallback && opt.suCallback();
            },
            fail: () => {
                opt && opt.failCallback && opt.failCallback();
            }
        })
    }

    /**添加桌面 */
    public addShortcut(opt: VideoCall) {
        CCGlobal.API.addShortcut({
            success: () => {
                opt && opt.suCallback && opt.suCallback();
            },
            fail: () => {
                opt && opt.failCallback && opt.failCallback();
            }
        })
    }

    public toSidebar(opt: VideoCall): void {
        CCGlobal.API.addCommonUse({
            success: () => {
                opt && opt.suCallback && opt.suCallback();
            }
        })
    }

    public createInterstitial(opt: VideoCall): void {
        this.interstitialAd.adCall = opt;
        this.interstitialAd.show();
    }

}

class video extends AdsBase {
    constructor(id) {
        super()
        this.id = id;
    }

    public create() {
        this.ad = CCGlobal.API.createRewardedVideoAd({
            adUnitId: this.id
        })

        if (this.ad) {
            this.ad.onError(this._onError.bind(this));
            this.ad.onClose(this._onClose.bind(this));
        }
        CCComFun.CCLog("广告创建完毕")
    }

    public show() {
        if (!this.ad) {
            this.showToast();
            this.adCall && this.adCall.errCallback && this.adCall.errCallback();
            return;
        }

        this.videoShowing = true;
        this.videoShowing = true;
        let showPro = this.ad.show();
        showPro.then(() => {
            CCComFun.CCLog("[video] 视频广告展示成功");
        }).catch((err) => {
            CCComFun.CCLog("[video] 视频广告展示失败", err);
        })
    }

    private _onError(err) {
        CCComFun.CCLog("[video] 触发视频错误监听", err);
        this.videoShowing = false;
        this.showToast();
        this.adCall && this.adCall.errCallback && this.adCall.errCallback();
    }

    private _onClose(res) {
        this.videoShowing = false;
        if (res && res.isEnded || res === undefined) {
            this.adCall && this.adCall.suCallback && this.adCall.suCallback();
        } else {
            this.adCall && this.adCall.failCallback && this.adCall.failCallback();
        }
    }

    public destroy() {
        if (this.id) {
            this.ad.offError();
            this.ad.offClose();
            this.ad.destroy();
            this.ad = null;
            this.videoShowing = false;
        }
    }


}



export class KSRecorder extends RecorderBase {

    public init() {
        if (engine.cc_debug) { return };
        this.recorder = CCGlobal.API.getGameRecorder();
    }

    /**录制开始 */
    public recorderStart() {
        if (engine.cc_debug) { return };
        this.recorder.start()
    }

    /**停止录制并保存录像 */
    public recorderStop() {
        if (engine.cc_debug) { return };
        this.recorder.stop();
    }

    /**发布录像 */
    public publishVideo(opt: VideoCall) {
        if (engine.cc_debug) { return };
        this.recorder.publishVideo({
            callback: () => {
                opt && opt.suCallback && opt.suCallback();
            }
        })
    }






}


class interstitial extends AdsBase {
    constructor(id) {
        super();
        this.id = id;
    }

    public create() {
        this.ad = CCGlobal.API.createInterstitialAd({
            adUnitId: this.id
        });

        if (this.ad) {
            this.ad.onError(this._onError.bind(this));
            this.ad.onClose(this._onClose.bind(this));
        }
        CCComFun.CCLog("插屏广告创建完毕");
    }

    public show() {
        if (!this.ad) {
            this.adCall && this.adCall.errCallback && this.adCall.errCallback();
            return;
        }

        let showPro = this.ad.show();
        showPro.then(() => {
            CCComFun.CCLog("[interstitial] 插屏广告展示成功");
            this.adCall && this.adCall.suCallback && this.adCall.suCallback();
        }).catch((err) => {
            CCComFun.CCLog("[interstitial] 插屏广告展示失败", err);
            this.adCall && this.adCall.errCallback && this.adCall.errCallback();
        });
    }

    private _onError(err) {
        CCComFun.CCLog("[interstitial] 插屏广告错误监听", err);
        this.adCall && this.adCall.errCallback && this.adCall.errCallback();
    }

    private _onClose() {
        CCComFun.CCLog("[interstitial] 插屏广告关闭");
    }

    public destroy() {
        if (this.ad) {
            this.ad.offError();
            this.ad.offClose();
            this.ad.destroy();
            this.ad = null;
        }
    }
}